package com.bt7274.service.impl;

/**
 * @Data 2025/6/10 11:50
 * @Author hp
 * @Version 1.0
 * @Description UserTorrentLikeServiceImpl
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bt7274.mapper.UserTorrentLikeMapper;
import com.bt7274.pojo.UserTorrentLike;
import com.bt7274.service.UserTorrentLikeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserTorrentLikeServiceImpl implements UserTorrentLikeService {
    @Resource
    private UserTorrentLikeMapper userTorrentLikeMapper;

    @Override
    public void likeTorrent(String userId, Long torrentId) {
        if (!isUserLiked(userId, torrentId)) {
            UserTorrentLike like = new UserTorrentLike();
            like.setUserId(userId);
            like.setTorrentId(torrentId);
            userTorrentLikeMapper.insert(like);
        }
    }

    @Override
    public void unlikeTorrent(String userId, Long torrentId) {
        UserTorrentLike like = userTorrentLikeMapper.selectOne(
                new QueryWrapper<UserTorrentLike>().eq("user_id", userId).eq("torrent_id", torrentId)
        );
        if (like != null) {
            userTorrentLikeMapper.deleteById(like.getId());
        }
    }

    @Override
    public Long getTorrentLikeCount(Long torrentId) {
        return userTorrentLikeMapper.selectCount(
                new QueryWrapper<UserTorrentLike>().eq("torrent_id", torrentId)
        );
    }

    @Override
    public boolean isUserLiked(String userId, Long torrentId) {
        return userTorrentLikeMapper.selectOne(
                new QueryWrapper<UserTorrentLike>().eq("user_id", userId).eq("torrent_id", torrentId)
        ) != null;
    }
}